library(tidyverse)
library(lme4)
library(broom.mixed)

# reading in data ---------------------------------------------------------

tess <- read.csv(file = 'data_amerispeak.csv')
mturk <- read.csv(file = 'data_pilot_problem.csv')
mturk_policy <- read.csv(file = 'data_pilot_policy.csv')

# tables and figures from manuscript ---------------------------------------------------------

# table 1
tess %>%
  select(frameprob, issue, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frameprob, data=.)))

# table 2
tess %>%
  select(frameprob, issue, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name, issue) %>% 
  do(broom::tidy(lm(value ~ frameprob, data=.))) %>%
  filter(term=='frameprob')

# figure 1
figure1 <- tess %>%
  dplyr::select(framepol, issue, opinion, petition, volunteer, polindex) %>%
  pivot_longer(cols = opinion:polindex) %>%
  filter(!is.na(framepol)) %>% 
  group_by(name, issue) %>%
  summarise(gainframe = mean(value[framepol==0], na.rm=T),
            lossframe = mean(value[framepol==1], na.rm=T),
            pval = t.test(value ~ framepol)$p.value)

figure1 %>%
  mutate(name_clean = case_when(name=='opinion' ~ "Opinion",
                          name=='petition' ~ "Petition",
                          name=='volunteer' ~ "Volunteer",
                          name=='polindex' ~ "Index"),
         name_clean = paste0(name_clean, '\np=', round(pval,3)),
         issue_clean = case_when(issue==1 ~ "Hospital Deserts",
                          issue==2 ~ "Marine Debris",
                          issue==3 ~ 'Drinking Water',
                          issue==4 ~ 'High School Education')) %>%
  pivot_longer(cols = gainframe:lossframe, names_to = 'frame', values_to = 'mean') %>%
  mutate(frame_clean = case_when(frame=='gainframe' ~ "Gain Frame",
                          frame=='lossframe' ~ "Loss Frame")) %>%
  ggplot() + 
  geom_col(aes(x = name_clean, y = mean, fill = frame_clean), stat = 'identity',  position=position_dodge2(width=.7), width=.7) +
  geom_text(aes(x = name_clean, y = mean, col = frame_clean, label = round(mean,2), group = frame_clean), position = position_dodge(width = .7), vjust = -.5, size = 2) +
  facet_wrap(~issue_clean, scales='free_x') +
  scale_fill_grey() + 
  scale_color_grey(guide='none') + 
  coord_cartesian(ylim = c(1,7)) +
  labs(x = NULL, y = 'Mean Response (1-7)', fill=NULL, col = NULL) + 
  theme_classic() 
#ggsave('figure1.png', width=8, height=5)

# results reported in manuscript text ---------------------------------------------------------

# interaction between frame and issue (page 21)
tess %>%
  mutate(issue_mb = ifelse(issue==2, 1, 0)) %>%
  select(frameprob, issue_mb, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frameprob*issue_mb, data=.)))

# differences based on whether anger was asked first (footnote 11)
tess %>%
  select(frameprob, angerfirst, issue, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frameprob*angerfirst + as.factor(issue), data=.)))

tess %>%
  mutate(worry_first = orderworry==1) %>%
  select(frameprob, worry_first, issue, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frameprob*worry_first + as.factor(issue), data=.)))

# correlation between problem index and party ID (footnote 12)
tess %>%
  filter(!is.na(frameprob)) %>% 
  select(frameprob, issue, pid01, probindex) %>%
  group_by(frameprob, issue) %>%
  summarise(cor = cor(x = probindex, y = pid01, use = 'complete.obs')) %>%
  group_by(issue) %>%
  summarise(mean(cor))

tess %>%
  filter(!is.na(frameprob)) %>% 
  select(frameprob, issue, IDEO, probindex) %>%
  group_by(frameprob, issue) %>%
  summarise(cor = cor(x = probindex, y = IDEO, use = 'complete.obs'))%>%
  group_by(issue) %>%
  summarise(mean(cor))

mturk %>%
  group_by(issue) %>%
  do(tidy(lm(newindex ~ frame, data=.))) %>%
  filter(term=='frame') %>%
  mutate(estimate = estimate*-1)

mturk %>%
  filter(!is.na(frame)) %>% 
  select(frame, issue, pid7pt, newindex) %>%
  group_by(frame, issue) %>%
  summarise(cor = cor(x = newindex, y = pid7pt, use = 'complete.obs')) %>%
  group_by(issue) %>%
  summarise(cor = abs(mean(cor))) %>%
  left_join(mturk %>%
              group_by(issue) %>%
              do(tidy(lm(newindex ~ frame, data=.))) %>%
              filter(term=='frame') %>%
              mutate(estimate = estimate*-1) %>%
              select(term, framing_effect = estimate)) %>%
  summarise(cor = cor(x = cor, y = framing_effect, use = 'complete.obs'))
  

# correlations between partisanship and problem index (page 26 and footnote 13)
tess %>%
  filter(!is.na(frameprob)) %>% 
  select(frameprob, issue, PARTYID, anger, worry, serious, priority) %>%
  pivot_longer(cols = anger:priority) %>% 
  group_by(frameprob, issue, name) %>%
  summarise(cor = cor(x = value, y = PARTYID, use = 'complete.obs')) %>%
  mutate(group = ifelse(name=='priority', 'priority', 'other')) %>%
  group_by(issue, group, frameprob) %>% 
  summarise(cor = mean(cor)) %>%
  group_by(group) %>%
  summarise(cor = mean(abs(cor)))

mturk %>%
  filter(!is.na(frame)) %>% 
  select(frame, issue, pid7pt, feel_1, feel_2, feel_3, feel_4rev, ser, pri) %>%
  pivot_longer(cols = feel_1:pri) %>% 
  group_by(frame, issue, name) %>%
  summarise(cor = cor(x = value, y = pid7pt, use = 'complete.obs')) %>%
  mutate(group = ifelse(name=='pri', 'priority', 'other')) %>%
  group_by(issue, group, frame) %>% 
  summarise(cor = mean(cor)) %>%
  group_by(group) %>%
  summarise(cor = mean(abs(cor)))
  

# tables and figures from appendix ---------------------------------------------------------

# table A2.1
tess %>%
  select(frameprob, issue, anger, worry, serious, priority, probindex,
         marine, drinking, highschool, ORDER1, ORDER2, ORDER3) %>% 
  pivot_longer(cols = anger:probindex) %>%
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frameprob + marine + drinking + highschool + as.factor(ORDER1), data=.)))

# figure A2.1
lm_anger <- lm(anger ~ frameprob*orderanger, data=tess)
lm_worry <- lm(anger ~ frameprob*orderanger, data=tess)
lm_serious <- lm(anger ~ frameprob*orderanger, data=tess)
lm_priority <- lm(anger ~ frameprob*orderanger, data=tess)

tibble(issue = c(rep('anger',4), rep('worry', 4), rep('serious', 4), rep('priority', 4)),
       frameprob = rep(c(0,1,0,1), 4),
       orderanger = rep(c(1,1,4,4),4),
       mean = c(predict(lm_anger, newdata = expand.grid(frameprob = c(0,1), orderanger = c(1,4))),
                predict(lm_worry, newdata = expand.grid(frameprob = c(0,1), orderanger = c(1,4))),
                predict(lm_serious, newdata = expand.grid(frameprob = c(0,1), orderanger = c(1,4))),
                predict(lm_priority, newdata = expand.grid(frameprob = c(0,1), orderanger = c(1,4)))))

# table A2.2
tess %>%
  select(frameprob, issue, anger, worry, serious, priority, probindex) %>% 
  pivot_longer(cols = anger:probindex) %>%
  filter(!is.na(frameprob)) %>% 
  group_by(name, issue) %>%
  summarise(gainframe = mean(value[frameprob==0], na.rm=T),
            lossframe = mean(value[frameprob==1], na.rm=T),
            pval = t.test(value ~ frameprob)$p.value)

# table A6.1
mturk %>%
  select(frame, feel_1, feel_2, feel_3, feel_4rev, ser, pri, newindex, order, issue) %>%
  pivot_longer(cols = feel_1:newindex) %>% 
  group_by(name) %>% 
  do(broom::tidy(lm(value ~ frame + as.factor(issue) + as.factor(order), data=.))) %>%
  filter(term=='frame')

# figure A6.1
mturk %>%
  group_by(issue, frame) %>%
  summarise(mean = mean(newindex, na.rm=T)) 

# figure A6.2
mturk_policy %>%
  select(policy, polframe, index) %>% 
  group_by(policy) %>%
  summarise(gainframe = mean(index[polframe==0], na.rm=T),
            lossframe = mean(index[polframe==1], na.rm=T),
            pval = t.test(index ~ polframe)$p.value) %>%
  bind_rows(mturk_policy %>%
              select(polframe, index) %>% 
              summarise(gainframe = mean(index[polframe==0], na.rm=T),
                        lossframe = mean(index[polframe==1], na.rm=T),
                        pval = t.test(index ~ polframe)$p.value)) 


# table A7.1
tess %>%
  select(frameprob, issue, anger, worry, serious, priority, probindex,
         marine, drinking, highschool, ORDER1, ORDER2, ORDER3, 
         educ3, age3cat, pid3, risk3cat, needcog3) %>% 
  pivot_longer(cols = anger:probindex, names_to = 'outcome', values_to = 'dv') %>%
  pivot_longer(cols = educ3:needcog3, names_to = 'moderator', values_to = 'value') %>%
  group_by(outcome, moderator) %>% 
  do(broom::tidy(lm(dv ~ frameprob*as.factor(value), data=.)))

# table A6.2
mturk %>%
  select(frame, issue, feel_1, feel_2, feel_3, feel_4rev, ser, pri, newindex,
         educ3cat, age3cat, pid, issue, order, caseid) %>% 
  pivot_longer(cols = feel_1:newindex, names_to = 'outcome', values_to = 'dv') %>%
  pivot_longer(cols = educ3cat:pid, names_to = 'moderator', values_to = 'value') %>%
  group_by(outcome, moderator) %>% 
  do(broom::tidy(lmer(dv ~ frame*as.factor(value) + as.factor(issue) + as.factor(order) + (1|caseid), data=.)))


# figure A8.1
mturk %>%
  group_by(issue) %>% 
  do(broom::tidy(lm(newindex ~ frame, data=.))) %>%
  filter(term=='frame') %>%
  mutate(estimate = estimate*-1) %>% 
  left_join(mturk %>%
              group_by(issue) %>%
              summarise(anger = mean(feel_1, na.rm=T),
                        anger_t = mean(feel_1[frame==1], na.rm=T),
                        anger_c = mean(feel_1[frame==0], na.rm=T))) %>%
  ggplot(aes(x = anger, y = estimate)) + 
  geom_point() + 
  geom_linerange(aes(xmin = anger_t, xmax = anger_c)) + 
  geom_smooth(method='lm', se = F) + 
  theme_classic()

tess %>%
  group_by(issue) %>% 
  do(broom::tidy(lm(probindex ~ frameprob, data=.))) %>%
  filter(term=='frameprob') %>%
  left_join(tess %>%
              group_by(issue) %>%
              filter(!is.na(frameprob)) %>% 
              summarise(anger = mean(anger, na.rm=T))) %>%
  left_join(tess %>%
              group_by(issue, frameprob) %>%
              filter(!is.na(frameprob)) %>% 
              summarise(mean = mean(anger, na.rm=T)) %>%
              mutate(frameprob = ifelse(frameprob==0, 'anger_c', 'anger_t')) %>% 
              pivot_wider(names_from = frameprob, values_from = mean)) %>%
  ggplot(aes(x = anger, y = estimate)) + 
  geom_point() + 
  geom_linerange(aes(xmin = anger_t, xmax = anger_c)) + 
  geom_smooth(method='lm', se = F) + 
  theme_classic()

# table A8.1
mturk %>%
  filter(frame==1) %>%
  select(feel_1, feel_2, feel_3, feel_4rev, ser, pri, issue) %>%
  group_by(issue) %>% 
  summarise_all(mean, na.rm=T) %>%
  select(-issue) %>% 
  cor(use = "complete.obs")

# figure A8.2
tess %>%
  mutate(index_noanger = (worry + priority + serious)/3) %>%
  group_by(issue) %>% 
  do(tidy(lm_robust(index_noanger ~ frameprob, data=.))) %>%
  filter(term=='frameprob') %>%
  left_join(tess %>%
              group_by(issue) %>%
              summarise(avg_level = mean(anger, na.rm=T))) %>% 
  bind_rows(tess %>%
              mutate(index_noworry = (anger + priority + serious)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_noworry ~ frameprob, data=.))) %>%
              filter(term=='frameprob') %>%
              left_join(tess %>%
                          group_by(issue) %>%
                          summarise(avg_level = mean(worry, na.rm=T)))) %>%
  bind_rows(tess %>%
              mutate(index_noser = (anger + priority + worry)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_noser ~ frameprob, data=.))) %>%
              filter(term=='frameprob') %>%
              left_join(tess %>%
                          group_by(issue) %>%
                          summarise(avg_level = mean(serious, na.rm=T))))%>%
  bind_rows(tess %>%
              mutate(index_nopri = (anger + serious + worry)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_nopri ~ frameprob, data=.))) %>%
              filter(term=='frameprob') %>%
              left_join(tess %>%
                          group_by(issue) %>%
                          summarise(avg_level = mean(priority, na.rm=T)))) %>% 
  mutate(name = case_when(outcome=='index_noanger' ~ "Anger",
                          outcome=='index_nopri' ~ "Priority",
                          outcome=='index_noser' ~ "Seriousness",
                          outcome=='index_noworry' ~ "Worry")) %>%
  ggplot(.) +
  geom_hline(yintercept=0, lty=2, col='grey') + 
  geom_pointrange(aes(x = avg_level, y = estimate, ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error)) + 
  facet_wrap(~name) + 
  geom_smooth(aes(x=avg_level, y = estimate), method='lm', se=F, col='black') +
  geom_hline(yintercept=0, lty=2, col='grey') + 
  labs(y = 'Framing Effect on Index of Remaining Variables', x='Average Level of Variable', col='') + 
  #coord_flip() + 
  theme_classic()

mturk %>%
  mutate(index_noanger = (feel_2+pri+ser)/3) %>%
  group_by(issue) %>% 
  do(tidy(lm_robust(index_noanger ~ frame, data=.))) %>%
  filter(term=='frame') %>%
  left_join(mturk %>%
              group_by(issue) %>% 
              summarise(avg_level = mean(feel_1, na.rm=T))) %>% 
  bind_rows(mturk %>%
              mutate(index_noworry = (feel_1+pri+ser)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_noworry ~ frame, data=.))) %>%
              filter(term=='frame') %>%
              left_join(mturk %>%
                          group_by(issue) %>%
                          summarise(avg_level = mean(feel_2, na.rm=T)))) %>%
  bind_rows(mturk %>%
              mutate(index_noser = (feel_2+pri+feel_1)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_noser ~ frame, data=.))) %>%
              filter(term=='frame') %>%
              left_join(mturk %>%
                          group_by(issue) %>% 
                          summarise(avg_level = mean(ser, na.rm=T)))) %>%
  bind_rows(mturk %>%
              mutate(index_nopri = (feel_2+feel_1+ser)/3) %>%
              group_by(issue) %>% 
              do(tidy(lm_robust(index_nopri ~ frame, data=.))) %>%
              filter(term=='frame') %>%
              left_join(mturk %>%
                          group_by(issue) %>% 
                          summarise(avg_level = mean(pri, na.rm=T)))) %>%
  mutate(name = case_when(outcome=='index_noanger' ~ "Anger",
                          outcome=='index_nopri' ~ "Priority",
                          outcome=='index_noser' ~ "serness",
                          outcome=='index_noworry' ~ "Worry")) %>%
  mutate_at(vars(estimate, std.error), function(x){x*-1}) %>% 
  ggplot(.) +
  geom_hline(yintercept=0, lty=2, col='grey') + 
  geom_pointrange(aes(x = avg_level, y = estimate, ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error)) + 
  facet_wrap(~name) + 
  geom_smooth(aes(x=avg_level, y = estimate), method='lm', se=F, col='black') +
  geom_hline(yintercept=0, lty=2, col='grey') + 
  labs(y = 'Framing Effect on Index of Remaining Variables', x='Average Level of Variable', col='') + 
  #coord_flip() + 
  theme_classic()
